package com.itheima.d3_udp2;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;

/**
 * UDP服务端 ：接收数据 多发多收
 */
public class Server {
    public static void main(String[] args) throws Exception {
        System.out.println("=====服务端启动成功=====");
        //1.创建对象
        DatagramSocket socket = new DatagramSocket(10001);
        while (true) {
            //2.接收数据
            byte[] bytes = new byte[1024 * 32];
            DatagramPacket packet = new DatagramPacket(bytes, bytes.length);
            //接收
            socket.receive(packet);
            //解码
            String s = new String(packet.getData(), 0, packet.getLength());
            System.out.println("接收的数据是："+ s);
            System.out.println("对方的IP："+packet.getAddress().getHostAddress());
            System.out.println("对方的端口号："+packet.getPort());
            System.out.println("================================");

        }

    }
}
